<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function Admin()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('session');
		$this->load->model('Usermodel');
	}
	
	public function index()
	{
		$username = $this->session->userdata('username');
        if($username){
			if($username == 'admin')
			{
				$allUsers = $this->Usermodel->getConfirmedUsers();
				$data = array ('username' => $username,
							   'users' => $allUsers,
							   'title' => 'Confirmed Users:');
				$this->load->view('admin_view', $data);
			}
			else
			{
				redirect('');
			}
        }
        else{
            redirect('');
        }
	}
	
	public function confirmedUsers()
	{
		$username = $this->session->userdata('username');
		if($username){
			if($username != 'admin')
			{
				redirect('');
			}
        }
        else{
			redirect('');
        }
		// User is admin
		$allUsers = $this->Usermodel->getConfirmedUsers();
		$data = array ('username' => $username,
					   'users' => $allUsers,
					   'title' => 'Confirmed Users:');
		$this->load->view('admin_view', $data);
	}
	
	public function unconfirmedUsers()
	{
		$username = $this->session->userdata('username');
		if($username){
			if($username != 'admin')
			{
				redirect('');
			}
        }
        else{
			redirect('');
        }
		// User is admin
		$allUsers = $this->Usermodel->getUnconfirmedUsers();
		$data = array ('username' => $username,
					   'users' => $allUsers,
					   'title' => 'Unconfirmed Users:');
		$this->load->view('admin_view', $data);
	}
	
	public function deactivatedUsers()
	{
		$username = $this->session->userdata('username');
		if($username){
			if($username != 'admin')
			{
				redirect('');
			}
        }
        else{
			redirect('');
        }
		// User is admin
		$allUsers = $this->Usermodel->getDeactivatedUsers();
		$data = array ('username' => $username,
					   'users' => $allUsers,
					   'title' => 'Deactivated Users:');
		$this->load->view('admin_view', $data);
	}
	
	public function deleteUser()
	{
		$username = $this->session->userdata('username');
		if($username){
			if($username != 'admin')
			{
				redirect('');
			}
        }
        else{
			redirect('');
        }
		
		if (isset($_GET['id'])) {
			$userId = $_GET['id'];
			// Deactivate user
			$this->Usermodel->setIsActivated($userId, false);
			// Show deactivated users
			$allUsers = $this->Usermodel->getDeactivatedUsers();
			$data = array ('username' => $username,
						   'users' => $allUsers,
						   'title' => 'Deactivated Users:');
			$this->load->view('admin_view', $data);
		}
		else {
			redirect('');
		}
	}
	
	public function activateUser()
	{
		$username = $this->session->userdata('username');
		if($username){
			if($username != 'admin')
			{
				redirect('');
			}
        }
        else{
			redirect('');
        }
	
		if (isset($_GET['id'])) {
			$userId = $_GET['id'];
			// Activate user
			$this->Usermodel->setIsActivated($userId, true);
			// Show unconfirmed users
			$allUsers = $this->Usermodel->getUnconfirmedUsers();
			$data = array ('username' => $username,
						   'users' => $allUsers,
						   'title' => 'Confirmed Users:');
			$this->load->view('admin_view', $data);
		}
		else {
			redirect('');
		}
	}
	
}